
/***************************************************************************************
****************************************************************************************
* FILE		: key.c
* Description	: 
*			  
* Copyright (c) 2020 by Simlife. All Rights Reserved.
* 
* History:
* Version		Name			Date				Description
   0.1			Alen			2020/05/07			Initial Version
   
****************************************************************************************
****************************************************************************************/

#include "includes.h"

void initKeyControlPin(void)
{
	key1PortTypeControl |= key1Pin;	  // input
	key1PortPullupControl |= key1Pin; // pull up
	key1PortWakeUpControl |= key1Pin; // wake up

	key2PortTypeControl |= key2Pin;	  // input
	key2PortPullupControl |= key2Pin; // pull up
}

U8 keyScan(void)
{
	static U8 status, lastKey;
	static structKeyCount keyCount;
	U8 keyTemp = NULL_KEY;

	if (key1PortAndPin == 0)
	{
		keyTemp |= ON_OFF_KEY;
	}

	if (key2PortAndPin == 0)
	{
		keyTemp |= MODE_KEY;
	}

	if (keyTemp != NULL_KEY)
	{
		keyCount.release = releaseTime;
		switch (status)
		{
		case IDLE:
			keyCount.debounce = debounceTime;
			status = DEBOUNCE;
			break;

		case DEBOUNCE:
		case SHORT_PRESS:
		case LONG_PRESS:
			keyCount.debounce--;
			if (keyCount.debounce == 0)
			{
				if (status == DEBOUNCE)
				{
					status = SHORT_PRESS;
					keyCount.debounce = shortPressTime;
				}
				else if (status == SHORT_PRESS)
				{
					if (flagSystemStatus)
					{
						status = LONG_PRESS;
						keyCount.debounce = longPressTime;
						lastKey = keyTemp;
					}
					else
					{
						status = WAIT_RELEASE;
						return (keyTemp | shortFlag);
					}
				}
				else if (status == LONG_PRESS)
				{
					status = WAIT_RELEASE;
					return (keyTemp | longFlag);
				}
			}
			break;

		case WAIT_RELEASE:
			break;
		default:
			status = IDLE;
			break;
		}
	}
	else
	{
		if (keyCount.release != 0)
		{
			keyCount.release--;
			if (status == LONG_PRESS)
			{
				if (keyCount.debounce > 0)
				{
					status = WAIT_RELEASE;
					keyCount.debounce = 0;
					keyTemp = lastKey;

					return (keyTemp | shortFlag);
				}
			}
			lastKey = NULL_KEY;
			if (keyCount.release == 0)
			{
				status = IDLE;
			}
		}
	}
	return NULL_KEY;
}

void keyProcess(U8 keyValue)
{ // 10ms call
	if (keyValue == NULL_KEY)
	{
		return;
	}
	if (flagSystemStatus)
	{
		U8 buffer[4] = {0x00, 0x00, 0x00, 0x00};
		switch (keyValue)
		{
		case ON_OFF_KEY | shortFlag:
			send_command_to_bt(WAKE_COMMAND, buffer);
			break;

		case ON_OFF_KEY | longFlag:
			flagSystemStatus = OFF;
			systemPower(OFF);
			break;

		case MODE_KEY | shortFlag:
			modeChange();
			break;

		case MODE_KEY | longFlag:
			buffer[0] = 0x02;
			send_command_to_bt(KEY_COMMAND, buffer);
			break;

		default:
			break;
		}
	}
	else
	{
		if (keyValue == (ON_OFF_KEY | shortFlag))
		{
			flagSystemStatus = ON;
			systemPower(ON);
		}
	}
}
